//
//  XMFollowListViewController.m
//  XunMi
//
//  Created by MacBook on 2024/4/3.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMFollowListViewController.h"
#import "XMUserListTableViewCell.h"
#import "XMUserDetailViewController.h"
#import "XMConversationViewController.h"


@interface XMFollowListViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <XMUserModel *> *dataArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation XMFollowListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self followListData];
    [self setupRefresh];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.dataArray = [NSMutableArray array];
    self.page = 1;
}

#pragma mark - refresh
- (void)setupRefresh {
    XMWeakSelf(self);
    self.tableView.mj_header = [XMRefreshNormalHeader headerWithRefreshingBlock:^{
        XMStrongSelf(self);
        [self followListDataRefresh];
    }];
    
    XMRefreshBackFooter *footer = [XMRefreshBackFooter footerWithRefreshingBlock:^{
        XMStrongSelf(self);
        self.page += 1;
        [self followListData];
    }];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
}

- (void)followListDataRefresh {
    self.page = 1;
    [self.dataArray removeAllObjects];
    [self followListData];
}

- (void)followListData {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"page_no": @(self.page),
        @"page_size": @(10),
        @"search_module": @(self.index == 1 ? 2 : 1)
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/index/regard" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMUserModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
                self.tableView.statusTitleLabel.text = self.index == 1 ? @"还没有人关注你哦~" : @"您还没有关注Ta人哦～";
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:kNibWithName(kStringFromClass(XMUserListTableViewCell)) forCellReuseIdentifier:kStringFromClass(XMUserListTableViewCell)];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMUserListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kStringFromClass(XMUserListTableViewCell)];
    [cell setupCellWithUser:self.dataArray[indexPath.row] type:(XMUserListCellFollow)];
    XMWeakSelf(self);
    cell.actBlock = ^(XMUserModel * _Nonnull model, XMUserListCellType type) {
        XMStrongSelf(self);
        XMConversationViewController *conversationVC = [[XMConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:model.user_id];
        conversationVC.conversationType = ConversationType_PRIVATE;
        conversationVC.targetId = model.user_id;
        conversationVC.titleStr = model.showName;
        conversationVC.unReadMessage = 0;
        conversationVC.enableNewComingMessageIcon = YES; //开启消息提醒
        conversationVC.enableUnreadMessageIcon = YES;
        [self.navigationController pushViewController:conversationVC animated:YES];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XMUserModel *model = self.dataArray[indexPath.row];
    XMUserDetailViewController *vc = [[XMUserDetailViewController alloc] init];
    vc.userId = model.user_id;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kUserListCellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

#pragma mark - lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            _tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        }
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        // 解决在iOS11上朋友圈demo文字收折或者展开时出现cell跳动问题
        _tableView.estimatedRowHeight = CGFLOAT_MIN;
        _tableView.estimatedSectionFooterHeight = CGFLOAT_MIN;
        _tableView.estimatedSectionHeaderHeight = CGFLOAT_MIN;
        _tableView.rowHeight = UITableViewAutomaticDimension;
    }
    return _tableView;
}

#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
    return self.view;
}

@end
